
from fastapi import FastAPI, Body
from .executor import CodeExecutor
from .install import install_package
import os

# 配置
MAX_WORKERS = int(os.getenv("MAX_WORKERS", "10"))
WORKER_TIMEOUT = int(os.getenv("WORKER_TIMEOUT", "15"))

app = FastAPI()
executor = CodeExecutor(timeout=WORKER_TIMEOUT, max_workers=MAX_WORKERS)


@app.get("/health")
async def health_check():
    return "ok"


@app.post("/install")
async def install(
        package_name: str = Body(..., title='python包名', embed=True),
):
    result = install_package(package_name)
    return result


@app.post("/run")
async def execute_code(
        code: str = Body(..., description='执行python代码'),
        *args,
        **kwargs
):
    result = await executor.execute(code, *args, **kwargs)
    return result


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8194)
